import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;


public class SymbolTable {
	private HashMap<String, String> namesToTypes;
	private HashMap<String, Function> namesToFunctions;
	
	public SymbolTable() {
		namesToTypes = new HashMap<String, String>();
		namesToFunctions = new HashMap<String, Function>();
	}
	
	public void addVariable(String type, String name) {
		if(namesToTypes.containsKey(name)) {
			/* TODO: Do it by function or scope instead of just by name. */
			throw new RuntimeException("Variable identifier " + name + " already in use.");
		}
		namesToTypes.put(name, type);
	}
	
	public void addVariables(String type, List<String> names) {
		for(String name : names) {
			addVariable(type, name);
		}
	}
	
	public void addFunction(Function f) {
		final String name = f.getName();
		if(namesToFunctions.containsKey(name)) {
			throw new RuntimeException("Function identifier " + name + " already in use.");
		}
		namesToFunctions.put(name, f);
	}
	
	public void dump() {
		System.out.println("=== Symbol table dump ===");
		System.out.println("Variables: ");
		for(Entry<String, String> e : namesToTypes.entrySet()) {
			System.out.println(e.getValue() + " " + e.getKey());
		}
		System.out.println("Functions: ");
		for(Function e : namesToFunctions.values()) {
			System.out.println(e);
		}
	}
}
